package danny.hoi.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import danny.hoi.domain.export.cargo.*;
import danny.hoi.service.cargo.ExtCproductService;
import danny.hoi.service.cargo.FactoryService;
import danny.hoi.web.controller.base.BaseController;
import danny.hoi.web.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

//cargo/extCproduct/list.do?contractId=${o.contractId}&contractProductId=${o.id}

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController  extends BaseController {
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;
    /**
     * 1.分页显示
     * 2.请求地址：/cargo/extCproduct/list.do
     * 3.响应地址：WEB-INF/pages/cargo/extc/extc-list.jsp
     */
    @RequestMapping("/list")
    public String list(String contractId,
                       String contractProductId,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        //查询所有的工厂
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        //查出当前货物下所有的附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(contractProductId);
        PageInfo<ExtCproduct> pageInfo =
                extCproductService.findByPages(extCproductExample, pageNum, pageSize);


        request.setAttribute("pageInfo", pageInfo);
        request.setAttribute("factoryList", factoryList);
        request.setAttribute("contractId", contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-list";
    }

    /**
     * 1.保存数据
     * 2.请求地址：/cargo/extCproduct/edit.do
     * 3.响应地址：redirect:/cargo/extCproduct/list.do?contractId=
     */
    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) {
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());
        if (!StringUtils.isEmpty(productPhoto.getOriginalFilename())) {
            String imgSrc = "";
            try {
                imgSrc = "http://" + fileUploadUtil.upload(productPhoto);
                extCproduct.setProductImage(imgSrc);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(StringUtils.isEmpty(extCproduct.getId())){
            //添加
            extCproductService.save(extCproduct);
        }else{
            //更新
            if (extCproduct.getProductImage() != null) {
                ExtCproduct dbCp =
                        extCproductService.findById(extCproduct.getId());
                if (dbCp.getProductImage() != null) {
                    String key = dbCp.getProductImage().replaceAll("http://pw7yf5ava.bkt.clouddn.com/","");
                    fileUploadUtil.delete(key);
                }
            }
            extCproductService.update(extCproduct);
        }

        return "redirect:/cargo/extCproduct/list.do?contractId="
                + extCproduct.getContractId() + "&contractProductId=" + extCproduct.getContractProductId();
    }

    /**
     * 1.前往修改页面
     * 2.请求地址：/cargo/extCproduct/toUpdate.do?id=${o.id}&contractId=
     * ${contractId}&contractProductId=${o.contractProductId}"
     * 3.响应地址：WEB-INF/pages/cargo/product/product-update.jsp
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //查询附件信息
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct", extCproduct);

        //查询所有的工厂
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        request.setAttribute("factoryList", factoryList);
        return "cargo/extc/extc-update";
    }

    /**
     * 1.删除
     * 2.请求地址：cargo/extCproduct/delete.do?id=${o.id}&contractId=${o.contractId}
     * 3.响应地址：redirect:/cargo/extCproduct/list.do?contractId=
     */
    @RequestMapping("/delete")
    public String delete(String id, String contractId,String contractProductId) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="
                + contractId + "&contractProductId=" + contractProductId;
    }
}
